/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, SunWeber.Com.
 * @license			http://www.sunweber.com/license.htm
 * @link				http://www.sunweber.com/
 * @since				Version 1.1 Beta
 * 
 */

function check_sha1(str){
	var group_id = $("#group_id").val();
	if( group_id==0 ){
		$("#err_group_id").html('<img height="16" width="16" border="0" alt="否" src="images/admin/publish_x.png"/>用户组不能为空！');
		return false;
	}else{$("#err_group_id").html('');}
	var name = $("#name").val();
	if( name=='' ){
		$("#err_name").html('<img height="16" width="16" border="0" alt="否" src="images/admin/publish_x.png"/>用户名不能为空！');
		return false;
	}else{$("#err_name").html('');}
	var password = $("#password").val();
	if((!str && password!='')||(str)){
		if( password=='' || password.length<6 || password.length>18 ){
			$("#err_password").html('<img height="16" width="16" border="0" alt="否" src="images/admin/publish_x.png"/>密码不能为空，且必须在6-18位之间！');
			return false;
		}else{$("#err_password").html('');}
		var password2 = $("#password2").val();
		if( password!=password2 ){
			$("#err_password2").html('<img height="16" width="16" border="0" alt="否" src="images/admin/publish_x.png"/>两次密码不相同！');
			return false;
		}else{$("#err_password2").html('');}
		$("#password").val(hex_sha1(password));
	}
	$("#add_member").submit();
}

function mb_do(str){
	var ar = get_selected();
	if(ar==''){
		alert('错误的操作，未选择任何运行参数！');
		return false;
	}
	if(str=='delmb'){
		if(confirm('此操作不可恢复,是否确定删除？')==false){
			return false;
		}
	}
	var arString = ar.join('_');
	window.location=base_url+'?m=member&a='+str+'&ids='+arString;
}

function mb_validate(str){
	var ar = get_selected();
	if(ar==''){
		alert('错误的操作，未选择任何运行参数！');
		return false;
	}
	var arString = ar.join('_');
	$.ajax({
		type: "POST",
		url: base_url+"?m=member&a="+str[0]+"&type="+str[1],
		data: {'ids':arString},
		dataType:'json',
		success: function(rs){
			if(rs.ok){
				for(x in ar){
					if(str[1]=='validate'){
						$("#mb_"+ar[x]+" .validate img").replaceWith("<img height='16' width='16' border='0' alt='是' src='images/admin/tick.png'/>")
					}else if(str[1]=='unvalidate'){
						$("#mb_"+ar[x]+" .validate img").replaceWith("<img height='16' width='16' border='0' alt='否' src='images/admin/publish_x.png'/>")
					}else if(str[1]=='banspeak'){
						$("#mb_"+ar[x]+" .ban_speak img").replaceWith("<img height='16' width='16' border='0' src='images/admin/tick.png'/>")
					}else{
						$("#mb_"+ar[x]+" .ban_speak img").replaceWith("<img height='16' width='16' border='0' src='images/admin/publish_x.png'/>")
					}
				}
			}
		}
	});
}

var move_members = '';
function member_move_page(str){
	if(str==0){
		$("#member_move").hide();
		return true;
	}else{
		var ar = get_selected();
		if(ar==''){
			alert('错误的操作，未选择任何运行参数！');
			return false;
		}
		move_members = ar.join('_');
		$("#member_move").show();
	}
}

function article_move(){
	var group = $("#move_group").val();
	if(group==0){
		alert('请选择要文章要移动到的栏目！');
		return false;
	}
	$("#member_move").hide();
	window.location=base_url+'?m=member&a=move&ids='+move_members+'&gid='+group;
}

function add_group(str){
	var name = $("#add_group_"+str+" .name").val();
	var grade = $("#add_group_"+str+" .grade").val();
	var upgrade_points = $("#add_group_"+str+" .upgrade_points").val();
	var datas = {'name':name,'grade':grade,'upgrade_points':upgrade_points,'type':str};
	$.ajax({
		type: "POST",
		url: base_url+"?m=member&a=addgroup",
		data: {'data':JSON.stringify(datas)},
		dataType:'json',
		success: function(rs){
			if(rs.ok){
				$("#add_group_"+str).before('<tr id="mb_'+rs.id+'"><td><div><a href="member.php?id='+rs.id+'">'+rs.id+'</a></div></td><td><div><input type="text" name="group['+rs.id+'][name]" class="name tp6" value="'+name+'" onchange="checkname('+rs.id+');"></div></td><td><div><input type="text" name="group['+rs.id+'][grade]" class="grade tp4" value="'+grade+'" onchange="changebg('+rs.id+');"></div></td><td><div><input type="text" id="group['+rs.id+'][upgrade_points]" class="upgrade_points tp5" value="'+upgrade_points+'" onchange="changebg('+rs.id+');"></div></td><td><div><a href="'+base_url+'?m=member&a=editgroup&type='+str+'&id='+rs.id+'">编辑</a></div></td><td><div><a href="#" onclick="delete_gp('+rs.id+');">删除</a></div></td></tr>');
				onload_page();
			}else{
				alert('用户组添加失败，请联系管理员！');
			}
		}
	});
}

function delete_gp(id){
	if(confirm('此用户组下的用户将转移到默认组，此操作不可恢复,是否确定删除？')==false){
		return false;
	}
	$.ajax({
		type: "POST",
		url: base_url+"?m=member&a=delgroup",
		data: {'id':id},
		dataType:'json',
		success: function(rs){
			if(rs.ok){
				$("#mb_"+id).remove();
			}else{
				alert('用户组添加失败，请联系管理员！');
			}
		}
	});
}

function group_list(){
	var validate = $("#mb_group").val();
	if(validate==0)return false;
	window.location = base_url+'?m=member&a=index&type=group&id='+validate;
}

function validate_list(str){
	window.location = base_url+'?m=member&a=index&type=validate&id='+str;
}

function search_member(){
	var rule = $("#search_rule").val();
	var key = $("#search_key").val();
	if(rule==0){
		alert('请选择筛选规则！');
		return false;
	}
	if(key==''){
		alert('筛选值不能为空！');
		return false;
	}
	window.location = base_url+'?m=member&a=search&rule='+rule+'&key='+key;
}

function checkname(str){
	var name = $("#mb_"+str+" .name").val();
	var names = new Array();
	var i=0;
	$(".name").each(function(){
		if($(this).val()==name){i=i+1;}
	});
	if(i>1){
		alert('已经存在该用户组名！');
		return false;
	}
	changebg(str);
	return true;
}

function checkgrade(str){
	var grade = $("#mb_"+str+" .grade").val();
	var grades = new Array();
	var i=0;
	$(".grade").each(function(){
		if($(this).val()==grade){i=i+1;}
	});
	if(i>1){
		alert('已经存在该用户组等级号！');
		return false;
	}
	changebg(str);
	return true;
}

function changebg(str){
	$("#mb_"+str).css('background','#ffcb99');
}